package com.hkrt.demo.data.pref

import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import com.hkrt.demo.App
import com.hkrt.demo.data.network.ApiConstants
import com.hkrt.demo.presentation.utils.extensions.emptyString
import com.hkrt.demo.presentation.utils.extensions.unSafeLazy
import javax.inject.Inject
import javax.inject.Singleton

/**
 * @author  Chenjinguang
 * @date 29.07.2017.
 */
@Singleton
class Preferences @Inject constructor(private val app: Application) {

    private val SHARED_PREF_NAME = "Qpos"
    private val USER_LOGGED_IN = "user_logged_in"
    private val USER_TOKEN = "user_token"

    private val MERCHANT_NO = "merchantNo"
    private val TERMINAL_TYPE = "terminaltype"

    fun getPreferences(): SharedPreferences {
        return app.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)
    }

    private val sharedPreferences by unSafeLazy {
        app.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)
    }
    val isUserLoggedIn
        get() = sharedPreferences.getBoolean(USER_LOGGED_IN, false)
    val token: String
        get() = sharedPreferences.getString(USER_TOKEN,"" )//ApiConstants.TOKEN

    fun saveUserLoggedIn() {
        sharedPreferences.put {
            putBoolean(USER_LOGGED_IN, true)
        }
    }

    fun saveUserLoggedOut() {
        sharedPreferences.put {
            putBoolean(USER_LOGGED_IN, false)
        }
    }

    infix fun saveUserToken(token: String?) {
        sharedPreferences.put {
            putString(USER_TOKEN, token)
        }
    }

    fun deleteToken() {
        sharedPreferences.put {
            putString(USER_TOKEN, emptyString)
        }
    }

    fun setUserLoggedIn() {
        sharedPreferences.put {
            putBoolean(USER_LOGGED_IN, false)
        }
    }

    fun setUserLoggedOut() {
        sharedPreferences.put {
            putBoolean(USER_LOGGED_IN, false)
        }
    }

    fun setSharedPreferencesData(key: String, value: String?) {
        sharedPreferences.put {
            putString(key, value)
        }
    }

    fun setSharedPreferencesData(key: String, value: Long) {
        sharedPreferences.put {
            putLong(key, value)
        }
    }

    fun setSharedPreferencesData(key: String, value: Boolean) {
        sharedPreferences.put {
            putBoolean(key, value)
        }
    }

    fun setSharedPreferencesData(key: String, value: Int) {
        sharedPreferences.put {
            putInt(key, value)
        }
    }

    fun getStringSharedPreferencesData(key: String): String {
        return getPreferences().getString(key, "")
    }

    fun getBooleanSharedPreferencesData(key: String): Boolean {
        return getPreferences().getBoolean(key, false)
    }

    fun getIntSharedPreferencesData(key: String): Int {
        return getPreferences().getInt(key, -1)
    }

    fun getLongSharedPreferencesData(key: String): Long {
        return getPreferences().getLong(key, -1)
    }

    fun getMerchantNo():String{
        return sharedPreferences.getString(MERCHANT_NO,"")
    }

    fun getTerminalType():String{
        return sharedPreferences.getString(TERMINAL_TYPE,"")
    }
}

private inline fun SharedPreferences.put(body: SharedPreferences.Editor.() -> Unit) {
    val editor = this.edit()
    editor.body()
    editor.apply()
}